package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.STFilterEntity;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.request.WjjEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ScaleUtils;

import java.util.ArrayList;
import java.util.List;

public class CommercialConcreteFliterPopWindow3 extends BasePopWindow {
    private LinearLayout rootLayout;
    private TextView areaView;
    private View areaLine,toConfigureLine,serviceLine,gradeLine,distanceLine,otherLine;
    private TextView toConfigureView;
    private TextView serviceView;
    private TextView gradeView;
    private TextView okView;
    private TextView resetView;
    private TextView distanceView;
    private TextView priceView;
    private TextView otherView;

    private MaxRecyclerView areaRecyclerView;
    private MaxRecyclerView toConfigureRecyclerView;
    private MaxRecyclerView gradeRecyclerView;
    private MaxRecyclerView serviceRecyclerView;
    private MaxRecyclerView distanceRecyclerView;
    private MaxRecyclerView priceRecyclerView;
    private MaxRecyclerView othereRecyclerView;

    private MaterialFilterAdapter3 areaAdapter;
    private MaterialFilterAdapter3 gradeAdapter;
    private MaterialFilterAdapter3 toConfigureAdapter;
    private MaterialFilterAdapter3 serviceAdapter;
    private MaterialFilterAdapter3 distanceAdapter;
    private MaterialFilterAdapter3 priceAdapter;
    private MaterialFilterAdapter3 otherAdapter;
    private FilterResultClick filterResultClick;
    private MaterialFilterJson materialFilterJson;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;
    private List<DictEntity3> otherList;
    private List<MaterialFilterAdapter3> adapterList = new ArrayList<>();
    private int index;

    public interface FilterResultClick {
        //        void result(MaterialFilterJson materialFilterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray);
        void result(MaterialFilterJson materialFilterJson, List<DictEntity3> data, int position);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public CommercialConcreteFliterPopWindow3(Context context) {
        super(context, DIR_RIGHT_MATCH_HEIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_st_fliter3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        areaView = holder.getPop().findViewById(R.id.area_view);
        areaLine = holder.getPop().findViewById(R.id.area_line);
        toConfigureView = holder.getPop().findViewById(R.id.to_configure_view);
        toConfigureLine = holder.getPop().findViewById(R.id.to_configure_line);
        serviceView = holder.getPop().findViewById(R.id.service_view);
        serviceLine = holder.getPop().findViewById(R.id.service_line);
        gradeView = holder.getPop().findViewById(R.id.grade_view);
        gradeLine = holder.getPop().findViewById(R.id.grade_line);
        distanceView = holder.getPop().findViewById(R.id.distance_view);
        distanceLine = holder.getPop().findViewById(R.id.distance_line);
        priceView = holder.getPop().findViewById(R.id.price_view);
        otherView = holder.getPop().findViewById(R.id.other_view);
        otherLine = holder.getPop().findViewById(R.id.other_line);
        areaRecyclerView = holder.getPop().findViewById(R.id.area_list);
        gradeRecyclerView = holder.getPop().findViewById(R.id.grade_list);
        toConfigureRecyclerView = holder.getPop().findViewById(R.id.to_configure_list);
        serviceRecyclerView = holder.getPop().findViewById(R.id.service_list);
        distanceRecyclerView = holder.getPop().findViewById(R.id.distance_list);
        priceRecyclerView = holder.getPop().findViewById(R.id.price_list);
        othereRecyclerView = holder.getPop().findViewById(R.id.other_list);

        areaRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        gradeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        toConfigureRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        serviceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        distanceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        priceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        othereRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));

        //储仓
        areaAdapter = new MaterialFilterAdapter3(mContext, null);
        areaAdapter.setDefItem(0);
        areaAdapter.setItemSelect(true);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 dictEntity3, int position) {
                materialFilterJson.setAreaId(dictEntity3.getId());
                index = position;
            }
        });


        areaRecyclerView.setAdapter(areaAdapter);

        //强度等级
        gradeAdapter = new MaterialFilterAdapter3(mContext, null);
        gradeAdapter.setItemSelect(true);
        gradeAdapter.setDefItem(0);
        gradeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 dictEntity3, int position) {
                materialFilterJson.setStrengthId(dictEntity3.getName());
                if (position == 0) {
                    otherView.setVisibility(View.GONE);
                    othereRecyclerView.setVisibility(View.GONE);
                } else {
                    otherView.setVisibility(View.VISIBLE);
                    othereRecyclerView.setVisibility(View.VISIBLE);
                }
            }
        });
        gradeRecyclerView.setAdapter(gradeAdapter);

        //配置
        toConfigureAdapter = new MaterialFilterAdapter3(mContext, null);
        toConfigureAdapter.setItemSelect(true);
        toConfigureAdapter.setDefItem(-1);
        toConfigureAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setGradationId(materialFilterEntity.getId());

            }
        });
        toConfigureRecyclerView.setAdapter(toConfigureAdapter);

        //服务
        serviceAdapter = new MaterialFilterAdapter3(mContext, null);
        serviceAdapter.setItemSelect(true);
        serviceAdapter.setDefItem(-1);
        serviceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setServiceId(materialFilterEntity.getId());
            }
        });
        serviceRecyclerView.setAdapter(serviceAdapter);

        //距离
        distanceAdapter = new MaterialFilterAdapter3(mContext, null);
        distanceAdapter.setItemSelect(true);
        distanceAdapter.setDefItem(-1);
        distanceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
//                materialFilterJson.setDistanceId(materialFilterEntity.getId());
                materialFilterJson.setDistance(materialFilterEntity.getId());
            }
        });
        distanceRecyclerView.setAdapter(distanceAdapter);

        //价格
        priceAdapter = new MaterialFilterAdapter3(mContext, null);
        priceAdapter.setItemSelect(true);
        priceAdapter.setDefItem(-1);
        priceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setPriceSort(Integer.parseInt(materialFilterEntity.getId()));
            }
        });
        priceRecyclerView.setAdapter(priceAdapter);

        //外加剂
        otherAdapter = new MaterialFilterAdapter3(mContext, null);
        otherAdapter.setMultiSelect(true);
        otherAdapter.setDefItem(-1);
        otherAdapter.setOnItemMultiClick2(new BaseRecycleAdapter.OnItemMultiClick2<DictEntity3>() {
            @Override
            public void onItemMultiClick(List<DictEntity3> dictEntity3s, int position, ItemViewHolder holder) {
                if (otherAdapter.getDatas().get(position).isChildCheck()) {
                    otherAdapter.getDatas().get(position).setName(otherList.get(position).getName());
                    otherAdapter.getDatas().get(position).setId(otherList.get(position).getId());
                    otherAdapter.getDatas().get(position).setChildCheck(false);
                    return;
                }
                DictEntity3 rootDictEntity = otherAdapter.getDatas().get(position);
                if (rootDictEntity.isHaveSpec()) {
                    List<DictEntity3> childList = otherAdapter.getDatas().get(position).getDatas();
                    if (childList == null || childList.size() == 0) {
                        return;
                    }
                    otherAdapter.showChildWindow(holder, childList, position);
                }
            }
        });

        othereRecyclerView.setAdapter(otherAdapter);

        materialFilterJson = new MaterialFilterJson();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                List<DictEntity3> otherCheck = otherAdapter.getCheckDatas();
                List<String> otherIds = new ArrayList<>();
                List<WjjEntity> otherNames = new ArrayList<>();
                for (DictEntity3 dictEntity3 : otherCheck) {
                    otherIds.add(dictEntity3.getId());
                    WjjEntity wjjEntity = new WjjEntity();
                    wjjEntity.setAdmixtureName(dictEntity3.getName());
                    wjjEntity.setSpec("");
                    otherNames.add(wjjEntity);
                }

                materialFilterJson.setAdmixtureId(otherNames);
                if (filterResultClick != null) {
//                    filterResultClick.result(materialFilterJson, areaAdapter.getCheckDatas(), areaAdapter.getCheckList());
                    filterResultClick.result(materialFilterJson, areaAdapter.getCheckDatas(),index);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {
        for (MaterialFilterAdapter3 adapter : adapterList) {
            adapter.setDefItem(0);
        }
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        materialFilterJson.setMaterKey(null);
        materialFilterJson.setMaterValue(null);
        materialFilterJson.setBrandId(new ArrayList<>());

        //重置服务
        serviceAdapter.setDefItem(-1);
        materialFilterJson.setServiceId(null);

        //重置配置
        toConfigureAdapter.setDefItem(-1);
        materialFilterJson.setGradationId(null);

        //重置强度等级
        gradeAdapter.setDefItem(0);
        materialFilterJson.setStrengthId("0");

        //重置储仓库
        areaList.clear();
        areaAdapter.setDefItem(0);
        materialFilterJson.setAreaId("");
//        areaAdapter.resetCheckDatas();
        index = 0;

        //重置距离
        distanceAdapter.setDefItem(0);
        materialFilterJson.setDistanceId("");

        //重置价格排序
        priceAdapter.setDefItem(-1);
        materialFilterJson.setPriceSort(0);

        //重置外加剂
        setOtherData(otherList);
        materialFilterJson.setAdmixtureId(null);
        otherAdapter.resetCheckDatas();
    }

    public void setData(STFilterEntity data) {
        //储仓
        setAreaData(data.getArea());
        //强度等级
        setGradeData(data.getStrength());
        //配置
        setToConfigureData(data.getGradation());
        //服务
        setServiceData(data.getServiceType());
        //距离
        setDistanceData(data.getDistance());
        //价格排序
        setPriceData(data.getPrice());
        //外加剂
        setOtherData(data.getOtherList());
    }

    public void setAreaData(List<DictEntity3> data) {
        if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
            areaLine.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
            areaLine.setVisibility(View.VISIBLE);
        }
        areaAdapter.setDatas(data);
        materialFilterJson.setAreaId(data.get(0).getId());
    }

    public void setAreaDefault(DictEntity3 data, int position) {
//        areaAdapter.setCheckDefultDatas(data, checkBooleanArray);
        areaAdapter.setDefItem(position);
       /* areaList.clear();
        if (data != null)
            for (DictEntity3 filterEntity : data) {
                areaList.add(filterEntity.getId());
            }*/
        materialFilterJson.setAreaId(data.getId());
    }

    public void setGradeData(List<DictEntity3> data) {
        if (data.size() == 0) {
            gradeView.setVisibility(View.GONE);
            gradeLine.setVisibility(View.GONE);
        } else {
            gradeView.setVisibility(View.VISIBLE);
            gradeLine.setVisibility(View.VISIBLE);
        }
        gradeAdapter.setDatas(data);
    }

    public void setGradeDefault(int position, String id) {
        gradeAdapter.setDefItem(position);
        materialFilterJson.setStrengthId(id);
    }

    public void setToConfigureData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            toConfigureView.setVisibility(View.GONE);
            toConfigureLine.setVisibility(View.GONE);
        } else {
            toConfigureView.setVisibility(View.VISIBLE);
            toConfigureLine.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getName().equals("二级配")) {
                toConfigureAdapter.setDefItem(i);
                materialFilterJson.setGradationId(data.get(i).getId());
            }
        }
        toConfigureAdapter.setDatas(data);
    }

    public void setServiceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            serviceView.setVisibility(View.GONE);
            serviceLine.setVisibility(View.GONE);
        } else {
            serviceView.setVisibility(View.VISIBLE);
            serviceLine.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getName().equals("自卸")) {
                serviceAdapter.setDefItem(i);
                materialFilterJson.setServiceId(data.get(i).getId());
            }
        }
        serviceAdapter.setDatas(data);
    }

    public void setDistanceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            distanceView.setVisibility(View.GONE);
            distanceLine.setVisibility(View.GONE);
        } else {
            distanceView.setVisibility(View.VISIBLE);
            distanceLine.setVisibility(View.VISIBLE);
        }
        distanceAdapter.setStringName(1);
        distanceAdapter.setDatas(data);
    }

    public void setDistanceDefault(int position, String id) {
        distanceAdapter.setDefItem(position);
        materialFilterJson.setDistanceId(id);
    }

    public void setPriceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            priceView.setVisibility(View.GONE);
        } else {
            priceView.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getName().equals("从低到高")) {
                priceAdapter.setDefItem(i);
                materialFilterJson.setPriceSort(Integer.parseInt(data.get(i).getId()));
            }
        }
        priceAdapter.setDatas(data);
    }

    public void setOtherData(List<DictEntity3> data) {
        savePreOhterData(data);
        otherAdapter.setDatas(data);
    }

    private void savePreOhterData(List<DictEntity3> data) {
        if (data == null) {
            return;
        }
        otherList = new ArrayList<>();
        for (DictEntity3 dictEntity3 : data) {
            otherList.add(dictEntity3.clone());
        }
    }

    public void setSpecData(List<DictEntity3> data) {
        adapterList.clear();
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getName());
            materValueList.add("");
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        layoutParams.setMargins(ScaleUtils.px2dip(mContext, 11), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 11));
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(14);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_999999));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter3 childAdapter = new MaterialFilterAdapter3(mContext, null);
        childAdapter.setItemSelect(true);
        layoutParams.setMargins(ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 5), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 5));
        childAdapter.setDefItem(-1);
        childAdapter.setIndex(index);
        adapterList.add(childAdapter);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        childAdapter.setDatas(data);
        childAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getName());
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
